package com.umgsai.thread.thread60;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SaveAndRead {
	public static void main(String[] args) {
		try {//序列化对象到磁盘
			MyObject myObject = MyObject.getInstance();
			FileOutputStream fileOutputStream = new FileOutputStream(new File("/Users/shangyidong/Downloads/myObjectFile.txt"));
			ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutputStream);
			objectOutput.writeObject(myObject);
			objectOutput.close();
			fileOutputStream.close();
			System.out.println(myObject.hashCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
		/****************/
		try {//从磁盘反序列化到对象
			FileInputStream fileInputStream = new FileInputStream(new File("/Users/shangyidong/Downloads/myObjectFile.txt"));
			ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
			MyObject myObject = (MyObject) objectInputStream.readObject();
			objectInputStream.close();
			fileInputStream.close();
			System.out.println(myObject.hashCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
